﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.OleDb;
using System.Data;
/// <summary>
///Products 的摘要说明
/// </summary>
public class Products
{
	public Products()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}

    public int AddPro(int Ptypeid, string Pname, string Ptitle, string Pkey, string Pjj, string HtmlUrl, string Pconn, string Psimg, int Psort, string maodian)
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("Insert into [Product](Ptypeid,Pname,Ptitle,Pkey,Pjj,HtmlUrl,Pconn,Psimg,Psort,maodian) Values (@Ptypeid,@Pname,@Ptitle,@Pkey,@Pjj,@HtmlUrl,@Pconn,@Psimg,@Psort,@maodian)", myconn);
        mycmd.Parameters.AddWithValue("@Ptypeid", Ptypeid);
        mycmd.Parameters.AddWithValue("@Pname", Pname);
        mycmd.Parameters.AddWithValue("@Ptitle", Ptitle);
        mycmd.Parameters.AddWithValue("@Pkey", Pkey);
        mycmd.Parameters.AddWithValue("@Pjj", Pjj);
        mycmd.Parameters.AddWithValue("@HtmlUrl", HtmlUrl);
        mycmd.Parameters.AddWithValue("@Pconn", Pconn);
        mycmd.Parameters.AddWithValue("@Psimg", Psimg);
        mycmd.Parameters.AddWithValue("@Psort", Psort);
        mycmd.Parameters.AddWithValue("@maodian", maodian);
        int s = 0;
        try
        {
            myconn.Open();
            s = mycmd.ExecuteNonQuery();

        }
        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        myconn.Close();
        return s;
    }
    public int GetProCountForHtmlUrl(string HtmlUrl)
    {
        int s = 0;
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("select Count(*) from [Product] Where HtmlUrl='" + HtmlUrl + "'", myconn);
        try
        {
            myconn.Open();
            s = int.Parse(mycmd.ExecuteScalar().ToString());
        }

        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        myconn.Close();

        return s;
    }

    public int GetProCount()
    {
        int s = 0;
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("select Count(*) from [Product]", myconn);
        try
        {
            myconn.Open();
            s = int.Parse(mycmd.ExecuteScalar().ToString());
        }

        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        myconn.Close();

        return s;
    }

    public DataSet GetProPage(int SPage1, int SPage2, string Sname)
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("select * from [Product] Order By Pid DESC", myconn);
        OleDbDataAdapter dr = new OleDbDataAdapter(mycmd);
        DataSet da = new DataSet();
        try
        {
            myconn.Open();
            dr.Fill(da, SPage1, SPage2, Sname);
        }
        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        finally
        {
            myconn.Close();
        }
        return da;
    }

    public int DelProduct(int Pid)
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("delete from [Product] where Pid = @Pid ", myconn);
        mycmd.Parameters.AddWithValue("@Pid", Pid);
        int nResult = -1;
        try
        {
            myconn.Open();
            nResult = mycmd.ExecuteNonQuery();
        }
        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        finally
        {
            myconn.Close();
        }
        return nResult;
    }

    public OleDbDataReader GetProAll()
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("select * from [Product] Order By Pid DESC", myconn);
        OleDbDataReader dr = null;
        
        try
        {
            myconn.Open();
            dr = mycmd.ExecuteReader(CommandBehavior.CloseConnection);

        }
        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }

        return dr;
    }

    public OleDbDataReader GetProForPid(int pid)
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("select * from [Product] where Pid=" + pid + "", myconn);
        OleDbDataReader dr = null;
        try
        {
            myconn.Open();
            dr = mycmd.ExecuteReader(CommandBehavior.CloseConnection);

        }
        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }

        return dr;
    }
    public OleDbDataReader GetPtype()
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("select * from [Ptype] Order By Ptypeid ASC", myconn);
        OleDbDataReader dr = null;
        try
        {
            myconn.Open();
            dr = mycmd.ExecuteReader(CommandBehavior.CloseConnection);

        }
        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }

        return dr;
    }

    public int UPPPro(int Ptypeid, string Pname, string Ptitle, string Pkey, string Pjj, string HtmlUrl, string Pconn, string Psimg, int Psort, string maodian, int pid)
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("Update [Product] Set Ptypeid=" + Ptypeid + ",Pname='" + Pname + "',Ptitle='" + Ptitle + "',Pkey='" + Pkey + "',Pjj='" + Pjj + "',HtmlUrl='" + HtmlUrl + "',Pconn=@Pconn,Psimg='" + Psimg + "',Psort=" + Psort + ",maodian='" + maodian + "' where Pid=" + pid + "", myconn);
        //mycmd.Parameters.AddWithValue("@Ptypeid", Ptypeid);
        //mycmd.Parameters.AddWithValue("@Pname", Pname);
        //mycmd.Parameters.AddWithValue("@Ptitle", Ptitle);
        //mycmd.Parameters.AddWithValue("@Pkey", Pkey);
        //mycmd.Parameters.AddWithValue("@Pjj", Pjj);
        //mycmd.Parameters.AddWithValue("@HtmlUrl", HtmlUrl);
        mycmd.Parameters.AddWithValue("@Pconn", Pconn);
        //mycmd.Parameters.AddWithValue("@Psimg", Psimg);
        ////mycmd.Parameters.AddWithValue("@Psort", Psort);
        //mycmd.Parameters.AddWithValue("@maodian", maodian);
        int s = 0;
        try
        {
            myconn.Open();
            s = mycmd.ExecuteNonQuery();

        }
        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        myconn.Close();
        return s;
    }
}